using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Data.DTO.Hmw;
using net_work.IService.Hmw;

namespace net_work.Controllers.Hmw;

[Route("api/[controller]")]
[ApiController]
public class QuestionAnswerSubmissionController(
    ILogger<QuestionAnswerSubmissionController> logger,
    IQuestionAnswerSubmissionService questionAnswerSubmissionService) : ControllerBase
{
    /// <summary>
    /// 获取题目答案提交列表
    /// </summary>
    /// <param name="request">分页请求实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("list")]
    [Authorize]
    public async Task<IActionResult> ListQuestionAnswerSubmissions([FromBody] PagedRequest request)
    {
        logger.LogInformation("获取题目答案提交列表");
        var result = await questionAnswerSubmissionService.GetQuestionAnswerSubmissionsPagedAsync(request);
        return AjaxResult.Success(result);
    }

    /// <summary>
    /// 获取题目答案提交信息
    /// </summary>
    /// <param name="questionAnswerSubmissionId">题目答案提交ID</param>
    /// <returns>操作结果</returns>
    [HttpGet("get")]
    [Authorize]
    public async Task<IActionResult> GetQuestionAnswerSubmissionById([FromQuery] long questionAnswerSubmissionId)
    {
        logger.LogInformation("获取题目答案提交信息");
        return await questionAnswerSubmissionService.SelectByIdAsync(questionAnswerSubmissionId) is
            { } questionAnswerSubmission
            ? AjaxResult.Success(questionAnswerSubmission)
            : AjaxResult.Error();
    }

    /// <summary>
    /// 修改题目答案提交
    /// </summary>
    /// <param name="questionAnswerSubmissionUpdateRequest">修改题目答案提交实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("update")]
    [Authorize]
    public async Task<IActionResult> UpdateQuestionAnswerSubmission(
        [FromBody] QuestionAnswerSubmissionUpdateRequest questionAnswerSubmissionUpdateRequest)
    {
        logger.LogInformation("修改题目答案提交");
        var updateQuestionAnswerSubmissionId =
            await questionAnswerSubmissionService.UpdateQuestionAnswerSubmission(questionAnswerSubmissionUpdateRequest);
        return updateQuestionAnswerSubmissionId > 0
            ? AjaxResult.Success(new { updateQuestionAnswerSubmissionId })
            : AjaxResult.Error();
    }

    /// <summary>
    /// 删除题目答案提交
    /// </summary>
    /// <param name="ids">题目答案提交ID</param>
    /// <returns>操作结果</returns>
    [HttpPost("delete")]
    [Authorize]
    public async Task<IActionResult> DeleteQuestionAnswerSubmission([FromBody] List<long> ids)
    {
        logger.LogInformation("删除题目答案提交");
        return await questionAnswerSubmissionService.DeleteQuestionAnswerSubmissionByIds(ids) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }

    /// <summary>
    /// 提交答案
    /// </summary>
    /// <param name="questionAnswerSubmissionId">题目答案提交ID</param>
    /// <returns>操作结果</returns>
    [HttpPost("submit")]
    [Authorize]
    public async Task<IActionResult> Submit([FromBody] long questionAnswerSubmissionId)
    {
        return await questionAnswerSubmissionService.Submit(questionAnswerSubmissionId) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }
}